// 子弹类
export default class Bullet {
  static WIDTH = 5
  static HEIGHT = 15
  static SPEED = 7

  constructor(
    x,
    y,
    speedX = 0,
    speedY = Bullet.SPEED,
    type = 'player',
    damage = 1
  ) {
    this.x = x
    this.y = y
    this.width = Bullet.WIDTH
    this.height = Bullet.HEIGHT
    this.speedX = speedX
    this.speedY = type === 'player' ? -speedY : speedY // 玩家子弹向上，敌人子弹向下
    this.type = type // 'player', 'enemy', 'laser'
    this.damage = damage // 子弹伤害值
    this.isLaser = false // 是否是激光

    // 根据类型设置不同的颜色
    switch (type) {
      case 'player':
        this.color = '#ffff00' // 黄色
        break
      case 'enemy':
        this.color = '#ff0000' // 红色
        break
      case 'laser':
        this.color = '#ff00ff' // 紫色
        this.isLaser = true
        break
      default:
        this.color = '#ffff00'
    }

    // 粒子效果
    this.particles = []
    this.lastParticleTime = 0

    // 激光效果
    this.laserGlow = 0
    this.laserGlowDirection = 0.1
  }

  update() {
    // 更新位置
    this.x += this.speedX
    this.y += this.speedY

    // 更新粒子
    for (let i = this.particles.length - 1; i >= 0; i--) {
      this.particles[i].life -= 1
      this.particles[i].x += this.particles[i].vx
      this.particles[i].y += this.particles[i].vy

      if (this.particles[i].life <= 0) {
        this.particles.splice(i, 1)
      }
    }

    // 根据类型生成粒子
    if (this.type === 'player') {
      this.generatePlayerParticles()
    } else if (this.type === 'enemy') {
      this.generateEnemyParticles()
    } else if (this.type === 'laser') {
      this.updateLaserEffect()
    }
  }

  render(ctx) {
    // 根据子弹类型绘制不同的外观
    if (this.type === 'player') {
      this.renderPlayerBullet(ctx)
    } else if (this.type === 'enemy') {
      this.renderEnemyBullet(ctx)
    } else if (this.type === 'laser') {
      this.renderLaserBullet(ctx)
    }

    // 绘制粒子
    for (const particle of this.particles) {
      ctx.beginPath()
      ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2)
      ctx.fillStyle = `rgba(${particle.r}, ${particle.g}, ${particle.b}, ${particle.alpha})`
      ctx.fill()
    }
  }

  // 绘制玩家子弹
  renderPlayerBullet(ctx) {
    // 绘制子弹主体
    ctx.fillStyle = this.color
    ctx.fillRect(this.x, this.y, this.width, this.height)

    // 绘制子弹头部
    ctx.beginPath()
    ctx.moveTo(this.x, this.y)
    ctx.lineTo(this.x + this.width / 2, this.y - 5)
    ctx.lineTo(this.x + this.width, this.y)
    ctx.fillStyle = this.color
    ctx.fill()

    // 绘制子弹尾部
    ctx.beginPath()
    ctx.moveTo(this.x, this.y + this.height)
    ctx.lineTo(this.x + this.width / 2, this.y + this.height + 5)
    ctx.lineTo(this.x + this.width, this.y + this.height)
    ctx.fillStyle = '#ff6600'
    ctx.fill()
  }

  // 绘制敌人子弹
  renderEnemyBullet(ctx) {
    // 绘制子弹主体
    ctx.fillStyle = this.color
    ctx.beginPath()
    ctx.arc(
      this.x + this.width / 2,
      this.y + this.height / 2,
      this.width,
      0,
      Math.PI * 2
    )
    ctx.fill()

    // 绘制子弹内核
    ctx.fillStyle = '#ffcc00'
    ctx.beginPath()
    ctx.arc(
      this.x + this.width / 2,
      this.y + this.height / 2,
      this.width / 2,
      0,
      Math.PI * 2
    )
    ctx.fill()
  }

  // 绘制激光子弹
  renderLaserBullet(ctx) {
    // 绘制激光主体
    ctx.fillStyle = `rgba(255, 0, 255, ${0.7 + this.laserGlow * 0.3})`
    ctx.fillRect(this.x, this.y, this.width, this.height)

    // 绘制激光光晕
    ctx.beginPath()
    ctx.rect(this.x - 5, this.y - 5, this.width + 10, this.height + 10)
    ctx.fillStyle = `rgba(255, 0, 255, ${0.3 + this.laserGlow * 0.2})`
    ctx.fill()
  }

  // 更新激光效果
  updateLaserEffect() {
    // 更新激光光晕
    this.laserGlow += this.laserGlowDirection
    if (this.laserGlow >= 1 || this.laserGlow <= 0) {
      this.laserGlowDirection *= -1
    }
  }

  isOutOfScreen() {
    return (
      this.y < -this.height ||
      this.y > 640 ||
      this.x < -this.width ||
      this.x > 480
    )
  }

  // 生成玩家子弹粒子
  generatePlayerParticles() {
    const now = Date.now()
    if (now - this.lastParticleTime > 50) {
      this.lastParticleTime = now

      // 根据武器等级生成不同的粒子
      const numParticles = 2
      for (let i = 0; i < numParticles; i++) {
        const particle = {
          x: this.x + this.width / 2,
          y: this.y + this.height,
          size: Math.random() * 2 + 1,
          vx: (Math.random() - 0.5) * 2,
          vy: Math.random() * 2 + 1,
          life: 20,
          r: 255,
          g: Math.random() * 200 + 55,
          b: 0,
          alpha: 1,
        }
        this.particles.push(particle)
      }
    }
  }

  // 生成敌人子弹粒子
  generateEnemyParticles() {
    const now = Date.now()
    if (now - this.lastParticleTime > 50) {
      this.lastParticleTime = now

      const numParticles = 2
      for (let i = 0; i < numParticles; i++) {
        const particle = {
          x: this.x + this.width / 2,
          y: this.y,
          size: Math.random() * 2 + 1,
          vx: (Math.random() - 0.5) * 2,
          vy: -(Math.random() * 2 + 1),
          life: 20,
          r: 255,
          g: Math.random() * 100,
          b: Math.random() * 100,
          alpha: 1,
        }
        this.particles.push(particle)
      }
    }
  }
}
